﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using XQ.Framework.io.text;
using XQ.WeChatOfficialAccount.BLL.Sys;
using XQ.WeChatOfficialAccount.DAL;
using XQ.WeChatOfficialAccount.WebAPI.Filter;

namespace XQ.WeChatOfficialAccount.WebAPI.Controllers.System
{
    /// <summary>
    /// 字典分类
    /// </summary>
    //[FormAuthenticationFilter]
    public class DictCategoryController : ApiController
    {
        /// <summary>
        /// 获取字典类型列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/DictCategory/GetDictCategoryList")]
        public IEnumerable<Sys_DicCategory> GetDictCategoryList()
        {
            DictCategoryBll bll = new DictCategoryBll();
            return bll.GetDictCategoryList();
        }

        /// <summary>
        /// 根据ID获取字典类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/DictCategory/GetDictCategoryById")]
        public Sys_DicCategory GetDictCategoryById(int dictCategoryId)
        {
            DictCategoryBll bll = new DictCategoryBll();
            return bll.GetDictCategoryById(dictCategoryId);
        }

        /// <summary>
        /// 字典分类保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("Api/DictCategory/SaveDictCategory")]
        public string SaveDictCategory(Sys_DicCategory model)
        {
            try
            {
                DictCategoryBll bll = new DictCategoryBll();

                //判断是否重名
                if (!bll.IsHasSameName(model.Title, model.ID))
                {
                    if (bll.SaveDictCategory(model))
                    {
                        return "ok";
                    }
                }
                else
                {
                    return "HAVESAME";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return "ok";
        }

        /// <summary>
        /// 删除字典类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/DictCategory/DeleteDictCategory")]
        public string DeleteDictCategory(int dictCategoryId)
        {
            DictCategoryBll bll = new DictCategoryBll();
            if (bll.DeleteDictCategory(dictCategoryId))
            {
                return "success";
            }
            return "err";
        }

        /// <summary>
        /// 批量删除字典类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("Api/DictCategory/DelDictCategorys")]
        public bool DelDictCategorys(List<int> dictCategoryIds)
        {
            DictCategoryBll bll = new DictCategoryBll();
            return bll.DelDictCategorys(dictCategoryIds);
        }
    }
}